package com.dxs.Controller;

import com.dxs.Entity.EntityUserData;
import com.dxs.Entity.FireDataEntity;
import com.dxs.Entity.WuzhiEntity;
import com.dxs.Mapper.FilesMapper;
import com.dxs.Mapper.ServerMapper;
import com.dxs.Service.Impl.FileServiceImpl;
import com.dxs.enums.ResponseCode;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/server")
public class ServerController {

    @Resource
    private FileServiceImpl fileService;

    @Resource
    private FilesMapper filesMapper;

    @Resource
    private ServerMapper serverMapper;


    /**
     * 获取历史数据
     * @return
     */
    @PostMapping("/getHistoryList")
    public ResultBean getHistoryList(){
        List<FireDataEntity> list = serverMapper.getFireData(0);
        System.out.println(list);
        if (!list.isEmpty()){
            return ResultBean.getFireDataListSuccess("成功获取！",list);
        }else {
            return ResultBean.Error("获取失败");
        }

    }

    /**
     * 刷新当前数据
     * @return
     */
    @PostMapping("/getCurrentList")
    public ResultBean getCurrentList(){
        List<FireDataEntity> list = serverMapper.getFireData(1);
        int len = list.size();
        int currentIndex = new Random().nextInt(len);
        FireDataEntity fireData = list.get(currentIndex);
        if (fireData != null){
            return ResultBean.getFireDataSuccess("刷新当前数据成功！", fireData);
        }else {
            return ResultBean.Error("获取失败");
        }

    }

    /**
     * 物质估算
     * @param id
     * @return
     */
    @PostMapping("/MaterialCalculation")
    public ResultBean MaterialCalculation(@RequestParam("pointCount") float pointCount){

        int carCount = (int) Math.ceil(pointCount / 2000);
        System.out.println("请求的救援车数量" + carCount);
        int peopleCount = carCount * 6;
        int waterCount = carCount * 2200;
        int miehuoqiCount= carCount * 10;
        int shuidaiCount= carCount * 21;
        int dengCount = carCount * 3;
        WuzhiEntity wuzhi = new WuzhiEntity();
        wuzhi.setCarCount(carCount);
        wuzhi.setPeopleCount(peopleCount);
        wuzhi.setWaterCount(waterCount);
        wuzhi.setMiehuoqiCount(miehuoqiCount);
        wuzhi.setShuidaiCount(shuidaiCount);
        wuzhi.setDengCount(dengCount);
        ResultBean<WuzhiEntity> res = new ResultBean<>();
        return res.Success2("救援物资计算成功", wuzhi);
    }

    /**
     * 下载某条数据
     * @param id
     * @return
     */
    @PostMapping("/downloadData")
    public ResultBean downloadData(@RequestParam("id") String id){
        FireDataEntity fireData = serverMapper.getFireDataByID(id);
        System.out.println(fireData);
        if (fireData != null){
            return ResultBean.getFireDataSuccess("下载信息获取成功", fireData);
        }else {
            return ResultBean.Error("下载信息获取失败");
        }

    }


    @PostMapping("/logOut")
    public ResultBean logOut(@RequestParam("account") String account){
        int updateStatus =  serverMapper.updateUserStatus(account, 0);
        if (updateStatus > 0){
            return ResultBean.Success("退出成功");
        }else {
            return ResultBean.Error("获取失败");
        }

    }


}
